package com.atguigu.gmall.mq.receiver;

import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author: atguigu
 * @create: 2023-06-26 14:57
 */
@Slf4j
@Component
public class ConfirmReceiver {


    /**
     * 通过消费者创建交换机,队列 完成绑定
     *
     * @param data
     * @param channel
     * @param message
     */
    @SneakyThrows
    @RabbitListener(bindings = {@QueueBinding(
            exchange = @Exchange(value = "exchange.confirm", durable = "true"),
            value = @Queue(value = "confirm.queue", durable = "true"),
            key = "routing.confirm"
    )})
    public void receiverConfirmMessage(String data, Channel channel, Message message) {
        //1.处理业务
        if (StringUtils.isNotBlank(data)) {
            log.info("消费者监听到普通消息:{},模拟执行业务", data);
        }
        //2.手动应答 参数一:被投递消息标签Tag 从1开始依次递增 参数二:是否批量确认
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

    }


}
